package org.jeecg.modules.simulator.service;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.factory.PersonPathJsonFactory;
import org.jeecg.modules.simulator.jsonot.*;
import org.jeecg.modules.simulator.mapper.GetNumPeopEnStationMapper;
import org.jeecg.modules.simulator.mapper.GetRouteStatisticsMapper;
import org.jeecg.modules.simulator.mapper.PsgStationDistributionMapper;
import org.jeecg.modules.simulator.mapper.TrainPsgDistributionMapper;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.person.path.PersonPath;
import org.jeecg.modules.simulator.model.person.path.PersonPathItem;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class HistoryBizPersonLoaderService {




    @Resource
    GetRouteStatisticsMapper getRouteStatisticsMapper;
    @Resource
    PsgStationDistributionMapper psgStationDistributionMapper;
    @Resource
    GetNumPeopEnStationMapper getNumPeopEnStationMapper;
    @Resource
    TrainPsgDistributionMapper trainPsgDistributionMapper;



    @Setter
    @Getter
    //各站人员OD分配概率(List<ODProbabilityApiInfo> 是路径信息，一个od有多个路径)
    Map<String, List<ODProbabilityApiInfo>> odProbabilityApiInfoMap = new HashMap<>();





    //一个时间段内每秒刷卡进站人数
    @Getter
    Map<String, Map<String, NumPeopEnStationApiInfo>> numPeopEnStationApiInfoMap = new HashMap<>();


    public void loadNumPeopleEnByEndStation(String entryDate, String endTime) {
        List<NumPeopEnStationApiInfo> numPeopEnStationApiInfoList = getNumPeopEnStationMapper.getNumPeopEnByEndStation(entryDate, endTime);
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        numPeopEnStationApiInfoMap =
                numPeopEnStationApiInfoList.stream()
                        .collect(Collectors.groupingBy(
                                info -> {
                                    // 解析时间并格式化
                                    LocalDateTime dateTime = LocalDateTime.parse(info.getOdEntryTime(), inputFormatter);
                                    return dateTime.format(outputFormatter); // 转换为 "HH:mm:ss"
                                },
                                Collectors.toMap(
                                        NumPeopEnStationApiInfo::getOStationId,
                                        info -> info
                                )
                        ));
    }

    public void loadNumPeopleEnStation(String entryDate, String startTime, String endTime) {
        List<NumPeopEnStationApiInfo> numPeopEnStationApiInfoList = getNumPeopEnStationMapper.getNumPeopEnStation(entryDate, startTime, endTime);
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        // 转换 List 为 Map
        numPeopEnStationApiInfoMap =
                numPeopEnStationApiInfoList.stream()
                        .collect(Collectors.groupingBy(
                                info -> {
                                    // 解析时间并格式化
                                    LocalDateTime dateTime = LocalDateTime.parse(info.getOdEntryTime(), inputFormatter);
                                    return dateTime.format(outputFormatter); // 转换为 "HH:mm:ss"
                                },
                                Collectors.toMap(
                                        NumPeopEnStationApiInfo::getOStationId,
                                        info -> info
                                )
                        ));
    }


    public void getRouteStatistics(String entryDate, String startTime, String endTime) {
//        String entryDate1=entryDate.replace("-", "");
//        String startTime1=startTime.replace(":", "");
//        String endTime1=endTime.replace(":", "");
        List<ODProbabilityApiInfo> ODProbabilityApiInfoList = getRouteStatisticsMapper.getRouteStatistics(entryDate, startTime, endTime);
//        List<ODProbabilityApiInfo> ODProbabilityApiInfoList2=new ArrayList<>();
//        Set<String> stationSet = new HashSet<>(stationMap.keySet());
//        ODProbabilityApiInfoList.forEach(e -> {
//            String oStationId = e.getOStationId();
//            String dStationId = e.getDStationId();
//            // 检查 oStationId 和 dStationId 是否都不在 stationSet 中
//            if (!stationSet.contains(oStationId) && !stationSet.contains(dStationId)) {
//                ODProbabilityApiInfoList2.add(e);
//            }
//        });

        odProbabilityApiInfoMap = ODProbabilityApiInfoList.stream().collect(Collectors.groupingBy(e -> e.getOStationId() + "_" + e.getDStationId()));

    }




    public List<PsgStationDistributionInfo> getPsgStationDistributionInfoList(String dateStr) {
        return psgStationDistributionMapper.getPsgStationDistribution(dateStr);
    }
    public List<TrainPsgDistributionApiInfo> getTrainPsgDistribution(String dateStr) {
        return trainPsgDistributionMapper.getTrainPsgDistribution(dateStr);
    }








}
